package com.andaily.web.controller.mobile.log;

import com.andaily.domain.dto.log.LogCommentDto;
import com.andaily.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * @author Shengzhao Li
 */
@Controller
@RequestMapping("/log/comment")
public class LogCommentController {

    @Autowired
    private LogService logService;

    @RequestMapping(value = "form", method = RequestMethod.GET)
    public String showForm(String guid, @RequestParam(required = true) String logGuid, Model model) throws Exception {
        LogCommentDto logCommentDto = logService.loadLogCommentDto(guid, logGuid);
        model.addAttribute("logCommentDto", logCommentDto);
        return "log/log_comment_form";
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String submit(@ModelAttribute("logCommentDto") LogCommentDto logCommentDto) throws Exception {
        logService.persistLogComment(logCommentDto);
        return "redirect:../details/" + logCommentDto.getLogGuid();
    }


    @RequestMapping("{guid}")
    public String details(@PathVariable() String guid, ModelMap model) throws Exception {
        LogCommentDto logCommentDto = logService.loadLogCommentDto(guid);
        model.addAttribute("comment", logCommentDto);
        return "log/log_comment_details";
    }

    @RequestMapping("delete/{guid}/{logGuid}")
    public String delete(@PathVariable("guid") String guid, @PathVariable("logGuid") String logGuid) throws Exception {
        logService.archiveLogComment(guid);
        return "redirect:../../../details/" + logGuid;
    }
}
